package com.roger.myHome.util;

import java.io.File;
import java.io.IOException;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;

public class StringUtils {
	public static String getJSONString(Map<String, String> hashMap){
		  try {
		    ObjectMapper objectMapper = new ObjectMapper();  
		    String jsonString = objectMapper.writeValueAsString(hashMap);  
		    System.out.println(jsonString);
		    JsonNode node = objectMapper.readTree(jsonString);  
		  
		    // 输出结果转意，输出正确的信息  
		    System.out.println(node.get("password"));  
		    // 输出不转意,输出结果会包含""，这是不正确的，除非作为json传递，如果是输出结果值，必须如上一行的操作  
		    System.out.println(node.get("name")); 
		    return jsonString;
		  } catch (IOException e) {
			  return "";
		  }
	}
	
	public static String listToJson(List<?> objList){
		Gson gson = new Gson();
		 //System.out.println(str);
		 return gson.toJson(objList);
	}
	
    public static String javabeanToJson(Object obj) {
        Gson gson = new Gson();
        if (obj == null) {
            return null;
        }
        return gson.toJson(obj);
    }
    
    public static String getUUID(){
        UUID uuid=UUID.randomUUID();
        String str = uuid.toString(); 
        String uuidStr = str.replace("-", "");
        return uuidStr;
    }
    
    public static String[] getFileNameSuffix(String originFileName) {   
    	String[] res = new String[2];
    	int point = originFileName.lastIndexOf(".");
    	String fileName = originFileName.substring(0, point);
        String suffix = originFileName.substring(point + 1);  
        res[0] = fileName;
        res[1] = suffix;
        return res;
    }
    
    public static void renameFile(String path,String oldname,String newname,String uploadFileSuffix,String os){ 
        if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名 
        	if("windows".equalsIgnoreCase(os)){
                File oldfile = new File(path+"\\"+oldname); 
                File newfile = new File(path+"\\"+newname + "." + uploadFileSuffix); 
                if(!oldfile.exists()){
                    return;//重命名文件不存在
                }
                if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同，则不允许重命名 
                    System.out.println(newname+"已经存在！"); 
                else{ 
                    oldfile.renameTo(newfile); 
                } 
        	}else{
                File oldfile = new File(path+"/"+oldname); 
                File newfile = new File(path+"/"+newname + "." + uploadFileSuffix); 
                if(!oldfile.exists()){
                    return;//重命名文件不存在
                }
                if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同，则不允许重命名 
                    System.out.println(newname+"已经存在！"); 
                else{ 
                    oldfile.renameTo(newfile); 
                } 
        	}

        }else{
            System.out.println("新文件名和旧文件名相同...");
        }
    }
    
	public static String getSystemOS(){
		Properties props = System.getProperties();
		String os = props.getProperty("os.name").toLowerCase();
        System.out.println("操作系统的名称：" + os);
        //System.out.println("操作系统的版本：" + props.getProperty("os.version"));
        if(os.toLowerCase().indexOf("windows") >= 0){
        	return "windows";
        }else{
        	return "linux";
        }
	}
     
	public static <T> T parseJSON(String json, Class<T> clazz) {
		Gson gson = new Gson();
		T info = gson.fromJson(json, clazz);
		return info;
	}

	public static boolean isNull(String string){
        if (string == null || "".equals(string) || "null".equals(string)) {
            return true;
        }else {
            return false;
        }
    }

    static List<String> initList = new ArrayList<>();

	static {
	    initList.add("a");
	    initList.add("b");
    }

    public static void main1(String[] args) {
	    String[] array = {"a", "b", "c"};
        List<String> list = new ArrayList<>(Arrays.asList(array));

        List<String> oList = new ArrayList<>();
        oList.add("d");

        list.addAll(oList);
        System.out.println(list);

        ImmutableList<String> immutableList = ImmutableList.of("a", "b", "c");

        String ids = new String("111,22,33,44");

        String[] items = ids.split(",");
        List<Integer> appIdList = Stream.of(items).map(Integer::parseInt).collect(Collectors.toList());
        System.out.println(appIdList);
        System.out.println(getSystemOS());
    }

    public static void main(String[] args) {
	    String clientNo = "coop-1123";
        if (!clientNo.toUpperCase().startsWith("CUST-") && !clientNo.toUpperCase().startsWith("COOP-")) {
            System.out.println("error");
        } else {
            System.out.println("true");
        }
    }

    public static boolean isSpecialChar(String str) {
        String regEx = "[`~!#$%^&*=':<>/?~！@#%*+‘；”“’？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static Map jsonToMap(String str){
        //第一种方式
        Map<String, String> maps = (Map) JSON.parse(str);
        return maps;
    }

    public static List jsonToList(String json, Class beanClass) {
        return JSONObject.parseArray(json, beanClass);
    }

}
